<?php

class RecipientDeterminationRuleCollection extends AbstractRecipientDeterminationRule
{
  
  /**
   * @var array
   */
  protected $rules = array();

  /**
   * @param AbstractRecipientDeterminationRule $rule
   * @return integer
   */
  public function addRule (AbstractRecipientDeterminationRule $rule)
  {
    $i = count($rule);
    $this->setRule($i, $rule);
    return $i;
  }

  /**
   * @param integer $index
   * @param AbstractRecipientDeterminationRule $rule
   */
  public function setRule ($index, AbstractRecipientDeterminationRule $rule)
  {
    $this->rules[$index] = $rule;
  }

  /**
   * @param integer $index
   * @return AbstractRecipientDeterminationRule
   */
  public function getRule ($index)
  {
    return $this->rules[$index];
  }

  public function save ()
  {
    foreach ($this->rules as $rule)
    {
      $rule->save();
    }
  }

  public function setWorkflowStep (AbstractWorkflowStep $workflow_step)
  {
    foreach ($this->rules as $rule)
    {
      $rule->setWorkflowStep($workflow_step);
    }
  }

  public function calculateUserId ()
  {
    foreach ($this->rules as $rule)
    {
      $rule->calculateUserId();
    }
  }

  /**
   * @return string
   */
  public function serialize ()
  {
    $serialized = array();
    $serialized['options'] = $this->options;
    $serialized['rules'] = $this->rules;
    
    return serialize($serialized);
  }

  /**
   * @param string $serialized
   */
  public function unserialize ($serialized)
  {
    $unserialized = unserialize($serialized);
    
    $this->options = $unserialized['options'];
    $this->rules = $unserialized['rules'];
  }
}